﻿using MetroFramework.Forms;
using WeighSystem.Class;
using WeighSystem.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeighSystem.Interface;
using System.Threading;
using SerialPortLib2;
using System.IO.Ports;
using System.Runtime.InteropServices;
using WeighSystem.Messagebox;
using WeighSystem.LiteDB;
using WeighSystem.Regex;
using System.Diagnostics;
using System.IO.Pipes;
using System.IO;
using WeigSystem.Class;

namespace WeighSystem
{
    public partial class MainForm : MetroForm
    {
        pnlLogin _login = null;
        SerialPortHelper _meterSerial = null;
        SerialPortHelper _gunSerial = null;
        Mutex readDataMutex = new Mutex();
        String realTimeWeight = "0.00";
        String realTimeCode = "";

        static bool hasBlockMessage = false;
        Mutex BlockMessageMutex = new Mutex();
        static AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
        static Process pipeClient = new Process();
        RegexTool reg = new RegexTool();
        ScanerHook keyboardHook;
        static string pureWieghString = "0.00";
        static string pureWieghStringForDisplay = "0.00";
        Mutex weighDataListMutex = null;

        static List<string> weighDataList = new List<string>();

        //string[] str = { "(", ",",";","}","/","+","@","&","!","||","|","[",")",">","$","*","]","\","<<RS>>","<<GS>>","<<EOT>>",""};
        static string[] _regStr = new string[] { "(", ",", ";", "}", "/", "+", "@", "&", "!","|", "[", ")", ">", "$", "*", "]", "\\" };// <<RS>>","<<GS>>","<<EOT>>",""};
        public MainForm()
        {
            InitializeComponent();
            
            this.StyleManager = msmMain;
            this.Shown += MainForm_Shown;

            _login = new pnlLogin(this);
            _login.SettingClosed += _login_SettingClosed;
            _login.LogInSuccess += _login_LogInSuccess;
            //_login.Swipe();

           
            this.StyleManager.Theme = Settings.Default.Theme;
            this.StyleManager.Style = Settings.Default.Style;
            //load some data 
            List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindLastCount<DataTransferInfo>(11);
            metroGrid1.DataSource = list;



        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            StartSerialPortTask();
            //startVoiceSpeechClient();
            Thread meterDT = new Thread(meterDataProcessorThread);
            meterDT.IsBackground = true;
            meterDT.Name = "meterDataProcessorThread";
            meterDT.Start();

            Thread meterTxt = new Thread(MeterDataInvoke);
            meterTxt.IsBackground = true;
            meterTxt.Name = "MeterDataInvoke";
            //meterTxt.Start();

            //FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "打开键盘钩子：");
            //keyboardHook = new ScanerHook();
            //keyboardHook.ScanerEvent += KeyboardHook_ScanerEvent;
            //keyboardHook.Start();



            // MotorZebraScannerNet scanner = new MotorZebraScannerNet();
            // scanner.BarcodeScannerInit();
            // scanner.DataReceived += Scanner_DataReceived;
        }
        #region
        /// <summary>
        /// out of date api
        /// </summary>
        /// <param name="e"></param>
        private void Scanner_DataReceived(string e)
        {
            String readString = e;
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码枪数据：" + readString);

            if (realTimeWeight == "0.00")
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.警告, "称重数据为空");
                //return;
            }

            this.Invoke((EventHandler)delegate
            {

                //if(readString.Contains("ST") && readString.Contains("kg"))
                //txtGun.Text = readString;
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码枪数据：" + readString);
                realTimeCode = getBoxCodeFromScanCode(readString);
                txtGun.Text = realTimeCode;
                if (realTimeCode != null)
                {
                    readDataMutex.WaitOne();
                    string getWeight = realTimeWeight;
                    readDataMutex.ReleaseMutex();
                    WeigSystem.Class.EnumRet retcode = WeigSystem.Class.EnumRet.SUCCESS;
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "开始发送数据到服务器: " + realTimeCode + "+" + getWeight);
                    if (retcode == SoapConnection.WebStartRequest(Settings.Default.Server, realTimeCode, getWeight).localError)
                    {
                        //ShowMessage show = new ShowMessage();
                        //show.ShowMessageBoxTimeout("发送成功", "消息", MessageBoxButtons.OK, 1000); //单位毫秒
                        txtSendResult.ForeColor = Color.LimeGreen;
                        txtSendResult.Text = "-> 成功";
                        startSpeech("成功");
                        realTimeWeight = "0.00";
                    }
                    else
                    {
                        txtSendResult.ForeColor = Color.Red;
                        txtSendResult.Text = "-> 失败";
                        startSpeech("失败");
                        return;
                    }
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "写数据到DB ");
                    LiteDBOP.Save(new DataTransferInfo
                    {
                        Weight = float.Parse(getWeight),
                        ScanCode = realTimeCode,
                        IsSuccess = retcode == WeigSystem.Class.EnumRet.SUCCESS ? true : false,
                        UpdaloadTime = DateTime.Now,
                        ErrorInfo = null,
                    });
                    //List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindAll<DataTransferInfo>();
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "更新显示 ");
                    List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindLastCount<DataTransferInfo>(11);
                    metroGrid1.DataSource = list;
                }
            }
        );
        }
        #endregion
        private void KeyboardHook_ScanerEvent(ScanerHook.ScanerCodes codes)
        {
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码信息："+codes.ToString());
            this.Invoke((EventHandler)delegate
            {
                txtMeter.Text = codes.ToString();
            });
        }

        private void StartSerialPortTask()
        {

            string[] str = SerialPort.GetPortNames();
            //if (str.Contains(Settings.Default.COMPort1) &&str.Contains(Settings.Default.ComPort2))
            try
            {
                if (true)//(str.Contains(Settings.Default.COMPort1))
                {
                    new Thread(() => { _start_SerialPort(); }).Start();
                }
                else
                {
                    string _message = "设备配置异常！\n请先查看电脑设备管理器中设备对应的串口号，然后在该软件设置界面设置正确的串口号和波特率，然后重启软件!";
                    // MessageBoxIcon _icon = MessageBoxIcon.Warning;
                    //MetroFramework.MetroMessageBox.Show(this, _message, Application.ProductName, MessageBoxButtons.OK, _icon);
                    MessageBox.Show(_message);
                }
            }
            catch (Exception err)
            {

                MessageBox.Show(err.Message);
            }
         

        }
        void _start_SerialPort()
        {
            Thread.Sleep(1500);

            /*
            _meterSerial = new SerialPortInput();
            _meterSerial.ConnectionStatusChanged += _meterSerial_ConnectionStatusChanged;
            _meterSerial.MessageReceived += _meterSerial_MessageReceived;
            _meterSerial.SetPort(Settings.Default.COMPort1, int.Parse(Settings.Default.BaudeRate1));
            _meterSerial.Connect();
            */
            _meterSerial = new SerialPortHelper(Settings.Default.COMPort1, int.Parse(Settings.Default.BaudeRate1),true);

            _meterSerial.DataReceived += _meterSerial_DataReceived;
            try
            {
                _meterSerial.Open();
            }
            catch (Exception err)
            {

                MessageBox.Show(err.Message);
            }

            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "开始打开秤串口...");
            if (_meterSerial.IsOpen)
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "打开秤串口成功");
                txtMeter.ForeColor = Color.LimeGreen;
                string readString = "ST,NT,+ 0.00 kg";
                if (readString.Contains("ST") && readString.Contains("kg"))
                {
                    //if (this.IsHandleCreated)
                    {
                        this.Invoke((EventHandler)delegate
                        {
                            txtMeter.Text = readString.Split(' ')[1];
                        });
                    }

                }    
            }
            _gunSerial = new SerialPortHelper(Settings.Default.ComPort2, int.Parse(Settings.Default.BaudeRate2));
            _gunSerial.DataReceived += _gunSerial_DataReceived;
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "开始打开扫码枪串口...");
            try
            {
                _gunSerial.Open();
            }
            catch (Exception err2)
            {

                MessageBox.Show(err2.Message);
            }

            if (_gunSerial.IsOpen)
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "打开枪串口成功");
                txtGun.ForeColor = Color.LimeGreen;
            }
        }

        private string getBoxCodeFromScanCode(string scancode)
        {
            string code = null;
            int len = 0;
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息,"待处理的字符串："+scancode);
            code = scancode;
            if (scancode.IndexOf("\n") > 0)
            {
                len = scancode.Split('\n').Count();
                //说明不是一个字符串
                if (len > 1)
                {
                    code = scancode.Split('\n')[len - 1].Trim('\r');
                }
                else if (len == 1)
                {
                    code = scancode.Trim('\r');
                }
                goto Over;
            }
            else if (scancode.Contains(" "))
            {
                code = scancode.Split(' ')[scancode.Split(' ').Count() - 1];
            }
            else if (scancode.Contains("#"))
            {
                code = scancode.Split('#')[scancode.Split('#').Count() - 1];
            }
            else if (scancode.Contains(";"))
            {
                code = scancode.Split(';')[scancode.Split(';').Count() - 1];
            }
            else if (scancode.Contains("<<RS>>"))
            {

                List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + "<<RS>>");
                code = list[0].ToString().Replace("<<RS>>", "");
            }
            else if (scancode.Contains("<<GS>>"))
            {
                reg = new RegexTool();
                List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + "<<GS>>");
                code = list[0].ToString().Replace("<<GS>>", "");
            }
            else if (scancode.Contains("<<EOT>>"))
            {
                reg = new RegexTool();
                List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + "<<EOT>>");
                code = list[0].ToString().Replace("<<EOT>>", "");
            }
            else if (scancode.Contains("||"))
            {
                reg = new RegexTool();
                List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + "||");
                code = list[0].ToString().Replace("||", "").Trim('|');
            }
            else if (scancode.Contains("\r\n"))
            {
                List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + "\r\n");
                code = list[0].ToString().Replace("\r\n", "");
            }
            else if (scancode.Contains("\r"))
            {
                string[] list = scancode.Split('\r');
                code = list[list.Length - 1];
            }
            else
            {
                foreach (var item in _regStr)
                {
                    if (scancode.Contains(item))
                    {
                        reg = new RegexTool();
                        List<string> list = reg.GetStringByReg(scancode, ".*?" + "\\" + item);
                        code = list[0].ToString().Replace(item, "");
                        break;
                    }
                }

            }
            Over:
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息,"解析后的字符串："+scancode);
            return  code;
        }
        private void startSpeech(string args)
        {
            // first method
            /*
            StreamWriter sw = new StreamWriter(pipeServer);
            {
                sw.AutoFlush = true;
                // Send a 'sync message' and wait for client to receive it.
                sw.WriteLine(args);
              
            }
            pipeServer.WaitForPipeDrain();
            */
            // second method

            using (SpeechHelper sh = new SpeechHelper())
            {
                sh.StartSpeaker(args);
            }

        }
        private void startVoiceSpeechClient()
        {
            pipeClient.StartInfo.FileName = Application.StartupPath + "\\SpeechExe" + "\\Speech.exe";

            pipeClient.StartInfo.Arguments =
                pipeServer.GetClientHandleAsString();
            pipeClient.StartInfo.CreateNoWindow = true;
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();
            pipeServer.DisposeLocalCopyOfClientHandle();

            //process.WaitForExit();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        private void _gunSerial_DataReceived(SerialPortHelper.DataReceivedEventArgs e)
        {
            String readString = System.Text.Encoding.Default.GetString(e.DataReceived.ToArray(), 0, e.DataReceived.Count());
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码枪原始数据：" + readString);

            if (realTimeWeight == "0.00")
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.警告, "称重数据为空");
                //return;
            }
            if (hasBlockMessage)
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "服务器返回的错误弹窗没有处理！丢弃当前码值=" + readString);
                return;
            }

            this.Invoke((EventHandler)delegate
            {

                hasBlockMessage = true;
                //if(readString.Contains("ST") && readString.Contains("kg"))
                //txtGun.Text = readString;
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码枪数据：" + readString);
                realTimeCode = getBoxCodeFromScanCode(readString);
                txtGun.Text = realTimeCode;

                if (realTimeCode != null)
                {
                    readDataMutex.WaitOne();
                    string getWeight = realTimeWeight;
                    readDataMutex.ReleaseMutex();
                    string _message = "远程服务器响应成功";
                    MessageBoxIcon _icon = MessageBoxIcon.Information;
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "开始发送数据到服务器: " + realTimeCode + "+" + getWeight);
                    S_RetMessage ret = SoapConnection.WebStartRequestTest(Settings.Default.Server, realTimeCode, getWeight);
                    if (ret.localError == EnumRet.SUCCESS)
                    {
                        //ShowMessage show = new ShowMessage();
                        //show.ShowMessageBoxTimeout("发送成功", "消息", MessageBoxButtons.OK, 1000); //单位毫秒
                        txtSendResult.ForeColor = Color.LimeGreen;
                        txtSendResult.Text = "-> 成功";
                        startSpeech("成功");
                        realTimeWeight = "0.00";
                        hasBlockMessage = false;
                    }
                    else
                    {
                        txtSendResult.ForeColor = Color.Red;
                        txtSendResult.Text = "-> 失败";
                        startSpeech("失败");
                        _icon = MessageBoxIcon.Warning;
                        hasBlockMessage = true;
                        if (ret.messager == null || ret.messager == "")
                            ret.messager = "服务器返回消息为空，返回状态值为 " + ret.status;
                        if (MetroFramework.MetroMessageBox.Show(this, ret.messager, Application.ProductName, MessageBoxButtons.OK, _icon) == DialogResult.OK)
                        {
                            hasBlockMessage = false;
                            return;
                        }
                        
                    }
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "写数据到DB ");
                    LiteDBOP.Save(new DataTransferInfo
                    {
                        Weight = float.Parse(getWeight),
                        ScanCode = realTimeCode,
                        IsSuccess = ret.localError == WeigSystem.Class.EnumRet.SUCCESS ? true : false,
                        UpdaloadTime = DateTime.Now,
                        ErrorInfo = null,
                    });
                    //List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindAll<DataTransferInfo>();
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "更新显示 ");
                    List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindLastCount<DataTransferInfo>(11);
                    metroGrid1.DataSource = list;
                }
            } 
        );
        }

        private void _meterSerial_DataReceived(SerialPortHelper.DataReceivedEventArgs e)
        {

            String readString = System.Text.Encoding.Default.GetString(e.DataReceived.ToArray(), 0, e.DataReceived.Count());
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到称重数据:" + readString);
            if (weighDataListMutex == null)
            {
                weighDataListMutex = new Mutex();
            }
            weighDataListMutex.WaitOne();
            weighDataList.Add(readString);
            weighDataListMutex.ReleaseMutex();
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "加入队列成功:" + readString);
        }

        void MeterDataInvoke()
        {
            Thread.Sleep(3000);
            while (true)
            {
                /*
                if (weighDataListMutex == null)
                {
                    weighDataListMutex = new Mutex();
                }
                weighDataListMutex.WaitOne();
                weighDataList.Add("wn00043.13kg");
                weighDataListMutex.ReleaseMutex();
                */
                if (pureWieghString == "0.00")
                    continue;
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "开始显示");
                this.Invoke((EventHandler)delegate
                {
                    try
                    {
                        if (float.Parse(pureWieghStringForDisplay) <= 0)
                        {
                            realTimeWeight = "0.00";
                            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "称重数据为小于0:" + float.Parse(pureWieghStringForDisplay).ToString());
                            Console.WriteLine(float.Parse(pureWieghStringForDisplay));
                        }
                        else
                        {
                            realTimeWeight = txtMeter.Text = (float.Parse(pureWieghStringForDisplay)).ToString("0.00");
                            //realTimeWeight = "0.00";

                        }
                    }
                    catch (Exception er)
                    {
                        FileLog.WriteLogToFile(FileLog.LOGLEVEL.错误, "数据转换出现错误!" + er.ToString());
                        throw;
                    }
                });
                Thread.Sleep(500);
            }
        }
        void meterDataProcessorThread()
        {
            int i = 0;
            string lastString = "";
            //weighDataList.Add("wn-0.013kg");
            //weighDataList.Add("wn0000.13kg");
            //weighDataList.Add("wn-0.200kg");
            while (true)
            {
                try
                {
                    if (weighDataList.Count > 0)
                    {
                        if (weighDataListMutex == null)
                        {
                            weighDataListMutex = new Mutex();
                        }

                        weighDataListMutex.WaitOne();

                        pureWieghString = weighDataList.ElementAt(weighDataList.Count-1);
                        if (lastString == pureWieghString)
                        {
                            weighDataList.RemoveAt(weighDataList.Count - 1);
                            weighDataListMutex.ReleaseMutex();
                            continue;
                        }
                            
                        FileLog.WriteLogToFile(FileLog.LOGLEVEL.错误, "从队列尾部取出数据!" + pureWieghString);
                        if (pureWieghString.Contains("w") && (pureWieghString.Contains("lb") || pureWieghString.Contains("kg")))
                        {
                            lastString = pureWieghString;
                            pureWieghStringForDisplay = pureWieghString.Substring(2,7);
                            /*
                            if (pureWieghStringForDisplay.Contains("lb"))
                            {
                                pureWieghStringForDisplay = pureWieghStringForDisplay.TrimEnd('b').TrimEnd('l').TrimStart('0');
                            }
                            else
                                pureWieghStringForDisplay = pureWieghStringForDisplay.TrimEnd('g').TrimEnd('k').TrimStart('0');
                            */
                            this.Invoke((EventHandler)delegate
                            {
                                try
                                {
                                    if (float.Parse(pureWieghStringForDisplay) <= 0)
                                    {
                                        realTimeWeight = "0.00";
                                        FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "称重数据为小于0:" + float.Parse(pureWieghStringForDisplay).ToString());
                                        Console.WriteLine(float.Parse(pureWieghStringForDisplay));
                                    }
                                    else
                                    {
                                        realTimeWeight = txtMeter.Text = (float.Parse(pureWieghStringForDisplay)).ToString("0.00");
                                        //realTimeWeight = "0.00";

                                    }
                                }
                                catch (Exception er)
                                {
                                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.错误, "数据转换出现错误!" + er.ToString());
                                    throw;
                                }
                            });



                            if (pureWieghStringForDisplay == null)
                            {
                                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "解析到数据为空" );
                            }
                            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "解析后的数字字符串为"+ pureWieghStringForDisplay);
                        }
                        weighDataList.RemoveAt(weighDataList.Count - 1);
                        weighDataListMutex.ReleaseMutex();

                    }
                }
                catch (Exception err)
                {
                    weighDataListMutex.ReleaseMutex();
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.错误, "数据解析出现错误!" + err.ToString());
                }
             
                Thread.Sleep(200);
            }
        }
        void _login_LogInSuccess(object sender, EventArgs e)
        {
            _login.Swipe(false);
        }

        void _login_SettingClosed(object sender, EventArgs e)
        {
            lnkSettings.Visible = true;
            lnlClose.Visible = true;
            _login.Swipe(false);
        }

        void MainForm_Shown(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //this.Bounds = Screen.PrimaryScreen.Bounds;
            this.Focus();
        }

        private void lnkSettings_Click(object sender, EventArgs e)
        {
            lnkSettings.Visible = false;
            lnlClose.Visible = false;
            _login.Swipe();
            _login.ShowSettings();
        }

        private void lnlClose_Click(object sender, EventArgs e)
        {
            if(_meterSerial != null)
                _meterSerial.Close();
            if (_gunSerial != null)
                _gunSerial.Close();
            //keyboardHook.Stop();
            Application.Exit();
        }

        private void minimizeLink_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void mtbSend_Click(object sender, EventArgs e)
        {
            //startSpeech("不支持");
            //System.Diagnostics.Process.Start(Application.StartupPath + "\\Speech.exe").WaitForExit();
            //SpeechSynthesizer synth = new SpeechSynthesizer();
            /*
            LiteDBOP.Save(new DataTransferInfo
            {
                Weight = 22.5f,
                ScanCode = "hahaha",
                IsSuccess = true,
                UpdaloadTime = DateTime.Now,
                ErrorInfo = null,
            });
            List<DataTransferInfo> list = (List<DataTransferInfo>)LiteDBOP.FindLastCount<DataTransferInfo>(1000);
            metroGrid1.DataSource = list;
            */

            //txtSendResult.ForeColor = Color.Red;
            //txtSendResult.Text = "-> 失败";
            //ShowMessage show = new ShowMessage();
            //show.ShowMessageBoxTimeout("发送成功", "消息", MessageBoxButtons.OK, 1000); //单位毫秒


            /*
            string str = "32232\r888888";
            List<string> list = reg.GetStringByReg(str, ".*?" + "\\" + "\r");
            string code = list[list.Count-1].ToString().Replace("\r", "");
            MessageBox.Show(getBoxCodeFromScanCode(code));
            */

            /*
            MessageBoxIcon _icon = MessageBoxIcon.Information;
            _icon = MessageBoxIcon.Warning;
            if (MetroFramework.MetroMessageBox.Show(this,"", Application.ProductName, MessageBoxButtons.OK, _icon) == DialogResult.OK)
            {
                return;
            }
            */
            /*
            string readString = "-0.001kg";
            readString = readString.TrimEnd('g').TrimEnd('k');
            if (float.Parse(readString) <= 0)
            {
                realTimeWeight = "";
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "称重数据为小于0:" + int.Parse(readString).ToString());
            }
            */
            /*
            string readString = "wn0009.23kg";
            readString = readString.Substring(2);
            if (readString.Contains("lb"))
            {
                readString = readString.TrimEnd('b').TrimEnd('l').TrimStart('0');
            }
            else
                readString = readString.TrimEnd('g').TrimEnd('k').TrimStart('0');
            if (readString == null)
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "解析到数据为空");
            }
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "数字字符串为" + readString);
            this.Invoke((EventHandler)delegate
            {
                try
                {
                    readDataMutex.WaitOne();
                    if (float.Parse(readString) <= 0)
                    {
                        realTimeWeight = "0.00";
                        FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "称重数据为小于0:" + float.Parse(readString).ToString());
                        Console.WriteLine(float.Parse(readString));
                    }
                    else
                    {
                        realTimeWeight = txtMeter.Text = (float.Parse(readString)).ToString("0.00");

                    }
                    readDataMutex.ReleaseMutex();
                }
                catch (Exception er)
                {
                    readDataMutex.ReleaseMutex();
                    FileLog.WriteLogToFile(FileLog.LOGLEVEL.错误, "数据转换出现错误!" + er.ToString());
                    throw;
                }
            });
            
            */
        }

    }
}
